<?php
/**
 * Author: FoxxxZhu
 * Date: 2016/11/2
 * Time: 11:01
 */

namespace Forxx;
use Lavender\Core;
use Lavender\Errno;
use Lavender\Exception;
use Lavender\Filter;
use \Lavender\WebService;

class ForxxApiService extends WebService{
    protected $ver = '';
    protected $app_id = '';
    protected $service = 'index.index';

    const REQUEST_TIME_KEY = '_rtk';


    public static function run($web_options)
    {
        /**
         * work环境不显示trace，但将耗时较高的存进log中
         * 体现在 success() , error()中
         * wuzhenqiu
         */
        self::$start_time = microtime(true);
// 		if(L_ENV == 'develop' || L_ENV == 'test' || L_ENV == 'work') {self::$start_time = microtime(true);}

        if (!is_array($web_options['action_modules']) ) {
            throw new Exception('action_modules not setted in $options', Errno::PARAM_INVALID);
        }

        //get route module & action
        $route_options = self::get_route_options();
        $module_name = empty($route_options['module']) ? 'index' : $route_options['module'];
        $action_name = empty($route_options['action']) ? 'index' : $route_options['action'];
        //module not found
        if (!in_array($module_name, $web_options['action_modules'])) {
            $web = new WebService();
            $web->render_error("{$module_name}.{$action_name}",404);
            return ;
        }

        //execute
        $class_name = "App\\Action\\{$module_name}";
        self::$instance = new $class_name($module_name, $web_options);
        self::$instance->execute($action_name);
    }

    /**
     * @return array
     */
    protected function get_request_param()
    {
        $request = array();
        if (!empty($_GET)) {
            foreach ($_GET as $key => $value) {
                $request[$key] = $value;
            }
        }
//        if (!empty($_POST)) {
//            foreach ($_POST as $key => $value) {
//                $request[$key] = $value;
//            }
//        }
        $row = self::get_raw_body();
        if(!empty($row))
        {
            $request['_input'] = $row;
        }
        return $request;
    }

}